package com.heima.article;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Mqtest {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void publishNow(){
        //测试已到时间需要发布的文章
        rabbitTemplate.convertAndSend("publish.article.topic",
                "publish.article",
                "d待发布文章id为1");
    }

    //todo 定时发布
    @Test
    public void publish3minLater(){
        rabbitTemplate.convertAndSend("publish.article.topic",
                "dead.publish.article",
                "消息发送时间：" + LocalDateTime.now().toString(), new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        MessageProperties messageProperties = message.getMessageProperties();
                        //todo 设置消息失效时间为60s，在消息到达失效时间后，消息标为死信
                        messageProperties.setExpiration("60000");
                        return message;
                    }
                });
    }

}
